<?php
if (!defined('FosfoaSystem'))
{
    exit(1);
}
class newsModel extends spModel
{
    public $pk = 'news_id';
    public $table = 'news';
    // 由spModel的变量$linker来设置表间关联
    var $linker = array(
    array('type' => 'hasone', // 关联类型，这里是一对一关联
        'map' => 'detail', // 关联的标识
        'mapkey' => 'news_id', // 本表与对应表关联的字段名
        'fclass' => 'news_descriptionModel', // 对应表的类名
        'fkey' => 'news_id', // 对应表中关联的字段名
        'enabled' => true // 启用关联
        ));
        
        
   function aliascheck($alias,$newsid)
    {
        $conditions=array('alias'=>$alias);
        $request=$this->find($conditions);
        if($request){
            if($request['news_id']==$newsid)
            {
                return true;
            }else{
                return false;
            }
        }else{
            return true;
        }
    }
    /**
     * 信息浏览统计
     */
     function hits($newsid)
     {
        $this->incrField(array('news_id'=>$newsid), 'hits');
     }
    /**
     * 根据条件返回信息列表
     */
    function News_list_for_conditions($conditions, $current_page, $page_size)
    {
        $fields1 = "news_id,news_image,alias,master_categories_id,news_date_added";
        $fields2 = "news_name,news_title,news_seokeywords,news_description,news_seodescription"; //这里限制被关联表的字段
        $order = "sort desc,news_id desc";
        $newsobj->linker[0]['field'] = $fields2;
        $request['newslist'] = $this->spLinker()->spPager($current_page, $page_size)->findAll($conditions, $order, $fields1);
        $request['pager'] = $this->spLinker()->spPager()->getPager();    
        return $request;
    }
    /**
     * 根据别名返回信息
     */
   function FindformAlias($alias)
    {
        $conditions=array('alias'=>$alias);
        $request=$this->spLinker()->spCache(3600 * 24)->find($conditions);
        if($request){
                return $request;
        }else{
            return false;
        }
    }
    /**
     * 根据ID返回信息
     */        
   function FindformID($news_id)
    {
        $conditions=array('news_id'=>$news_id);
        $request=$this->spLinker()->spCache(3600 * 24)->find($conditions);
        if($request){
                return $request;
        }else{
            return false;
        }
    }
    
    /**
     * 查询上一条
     */
     function Previous($cid,$newsid)
     {
       $fields= "news_name";
       $condition = 'master_categories_id=' . $cid .' and news_id<' . $newsid;
       $this->linker[0]['field'] = $fields;
       $request = $this->spLinker()->spCache(3600 * 24)->find($condition, 'news_id desc');
       if (!$request)
       {
         $condition = 'master_categories_id=' . $cid;
         $request = $this->spLinker()->spCache(3600 * 24)->find($condition, 'news_id desc');
       }
       return $request;
     }
    /**
     * 查询下一条
     */
     function Next($cid,$newsid)
     {
       $fields= "news_name";
       $condition = 'master_categories_id=' . $cid .' and news_id>' . $newsid;
       $this->linker[0]['field'] = $fields;
       $request = $this->spLinker()->spCache(3600 * 24)->find($condition, 'news_id asc');
       if (!$request)
       {
         $condition = 'master_categories_id=' . $cid;
         $request = $this->spLinker()->spCache(3600 * 24)->find($condition, 'news_id asc');
       }
       return $request;
     }
     
    /**
     * 获取最新的信息
     * @cid 如果为空就获取所有信息
     * @num 前几条
     * @order 排序
     */
    function get_news_list($cid,$num=10,$type=1)
    {
        switch ($type) {
            case 1: //最新
                $order='sort desc,news_id desc';
                break;
            case 2: //推荐
                $order='sort desc,news_id desc';
                break;
            case 3: //最热
                $order='hits desc,sort desc';
                break;
        }
        
        if($cid<>2 and $cid)
        {
            $conditions=($type==2)?array('master_categories_id'=>$cid,'type'=>1):array('master_categories_id'=>$cid);
        }else{
            $conditions=($type==2)?'master_categories_id<>1 and type=1':'master_categories_id<>1';
        }
        $fields2 = "news_name,news_url,news_description,news_seodescription,news_title,news_seokeywords"; //这里限制被关联表的字段
        $this->linker[0]['field'] = $fields2;
        $fields='news_id,news_image,news_date_added,master_categories_id,alias';
        $request = $this->spLinker()->spCache(3600 * 24)->findAll($conditions,$order,$fields,$num);
        return $request;
    }
}